import { Body, Controller, Header, Post, UploadedFiles, UseGuards, UseInterceptors } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { ApiOperation } from '@nestjs/swagger';
import { BasicService } from './basic.service';
import { build } from 'node-xlsx';

@Controller('api/basic')
@UseGuards(AuthGuard('jwt'))
export class BasicController {
    constructor(
        private readonly basicService: BasicService
    ) { }

    @Post("xlsx2data")
    @ApiOperation({ summary: "xlsx文件转数据" })
    @UseInterceptors(AnyFilesInterceptor())
    public async xlsx2data(
        @UploadedFiles() files
    ) {
        return this.basicService.xlsx2data(files);
    }

    @Post("data2xlsx")
    @ApiOperation({ summary: "数据转xlsx" })
    @Header("Content-Disposition", "attachment; filename=example.xlsx")
    @Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    public async data2xlsx(
        @Body() { name, data }
    ) {
        const xlsx = build([{
            name,
            data,
            options: {}
        }])
        return xlsx.toString("base64")
    }
}
